/
BinaryFileMessageSerializer`1.cs
69 lines (58 loc) · 2.59 KB
/
BinaryFileMessageSerializer`1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright (c) 2020 Sergio Aquilini
// This code is licensed under MIT license (see LICENSE file for details)
using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Threading.Tasks;
using Silverback.Messaging.Messages;
using Silverback.Messaging.Serialization;
using Silverback.Util;
namespace Silverback.Messaging.BinaryFiles
{
/// <summary>
/// Handles the <see cref="IBinaryFileMessage" />. It's not really a serializer, since the raw
/// binary content is transmitted as-is.
/// </summary>
/// <typeparam name="TModel">
/// The type of the <see cref="IBinaryFileMessage" /> implementation.
/// </typeparam>
public class BinaryFileMessageSerializer<TModel> : IMessageSerializer
where TModel : IBinaryFileMessage, new()
{
private readonly Type _type = typeof(TModel);
/// <inheritdoc cref="IMessageSerializer.RequireHeaders" />
public bool RequireHeaders => false;
/// <inheritdoc cref="BinaryFileMessageSerializer.SerializeAsync" />
[SuppressMessage("", "CA2000", Justification = "MemoryStream is being returned")]
public ValueTask<Stream?> SerializeAsync(
object? message,
MessageHeaderCollection messageHeaders,
MessageSerializationContext context)
{
Check.NotNull(messageHeaders, nameof(messageHeaders));
if (message == null)
return ValueTaskFactory.FromResult<Stream?>(null);
if (message is Stream inputStream)
return ValueTaskFactory.FromResult<Stream?>(inputStream);
if (message is byte[] inputBytes)
return ValueTaskFactory.FromResult<Stream?>(new MemoryStream(inputBytes));
var binaryFileMessage = message as IBinaryFileMessage;
if (binaryFileMessage == null)
{
throw new ArgumentException(
"The message is not implementing the IBinaryFileMessage interface.",
nameof(message));
}
return ValueTaskFactory.FromResult(binaryFileMessage.Content);
}
/// <inheritdoc cref="IMessageSerializer.DeserializeAsync" />
public ValueTask<(object? Message, Type MessageType)> DeserializeAsync(
Stream? messageStream,
MessageHeaderCollection messageHeaders,
MessageSerializationContext context)
{
var binaryFileMessage = new TModel { Content = messageStream };
return ValueTaskFactory.FromResult<(object?, Type)>((binaryFileMessage, _type));
}
}
}